package cn.wuzhizhan.reporter;

import com.codahale.metrics.*;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

/**
 * Created by wzz on 16-6-13.
 * Metrics 客户端
 */
public class Metrics  {
    private static final MetricRegistry metrics = new MetricRegistry();

    static {
        Graphite graphite = new Graphite(new InetSocketAddress("graphite.example.com", 2003));
        GraphiteReporter reporter = GraphiteReporter.forRegistry(metrics)
                .prefixedWith("web1.example.com")
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .filter(MetricFilter.ALL)
                .build(graphite);
        reporter.start(1,TimeUnit.SECONDS);
    }
    private Metrics() {
        //leve empty
    }
    public static Counter counter(String name) {
        return metrics.counter(name);
    }

    /**
     * Creates a new {@link Histogram} and registers it under the given name.
     *
     * @param name the name of the metric
     * @return a new {@link Histogram}
     */
    public static Histogram histogram(String name) {
        return metrics.histogram(name);
    }

    /**
     * Creates a new {@link Meter} and registers it under the given name.
     *
     * @param name the name of the metric
     * @return a new {@link Meter}
     */
    public static Meter meter(String name) {
        return metrics.meter(name);
    }

    /**
     * Creates a new {@link Timer} and registers it under the given name.
     *
     * @param name the name of the metric
     * @return a new {@link Timer}
     */
    public static Timer timer(String name) {
        return metrics.timer(name);
    }


    public static void main(String[] args) {
        Metrics.timer("hello");
    }

    private static void wait5Minutes() {
        try {
            Thread.sleep(5 * 1000 * 60);
        } catch (InterruptedException e) {
        }

    }

    static void wait5Seconds() {
        try {
            Thread.sleep(5 * 1000);
        } catch (InterruptedException e) {
        }
    }
}
